using System;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using SouthernLand.Common;

namespace SouthernLand.Controls
{
    public partial class LogonBox : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtEmail.Attributes.Add("onkeypress", "return clickButton(event,'" + btPro.ClientID + "')");
            txtPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btPro.ClientID + "')");
            PanelLogon.Visible = Globals.CurrentUserName == string.Empty;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btLogon.Click += new EventHandler(btLogon_Click);
        }

        protected void btLogon_Click(object sender, EventArgs e)
        {
            if (System.Web.Security.Membership.ValidateUser(txtEmail.Text, txtPassword.Text))
            {
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Session["username"] = txtEmail.Text.Trim();
                    Response.Redirect(Globals.ApplicationPath + "LogonRedirect.aspx");
                }
            }
            else
            {
                // kiem tra xem co UserName hay ko 
                if (txtEmail.Text == ConfigurationManager.AppSettings["EmailWebmaster"] &&
                    txtPassword.Text == ConfigurationManager.AppSettings["Password"])
                {
                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Session["username"] = txtEmail.Text.Trim();
                    Response.Redirect(Globals.ApplicationPath + "LogonRedirect.aspx");
                }
                else
                    lblMsg.Text = ResourceManager.GetString("Logon_False");
            }
        }

        protected void btDangky_Click(object sender, EventArgs e)
        {
            Response.Redirect(Globals.ApplicationPath + "Membership/CreateUser.aspx");
        }
    }
}